//
//  LCUserAmountViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/25.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCUserAmountViewController.h"
#import "LCUserModel.h"

#import "LCMineService.h"
#import "LCAmountListModel.h"
#import "LCAmountListTableViewCell.h"

#import "LCWithdrawalViewController.h"

@interface LCUserAmountViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *amountLb;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet XWXImgTitleBtn *affairsBtn;

@property (assign, nonatomic)NSInteger affairs;

@property (assign, nonatomic)NSInteger page;

@property (nonatomic, strong)NSMutableArray * dataArr;

@end

@implementation LCUserAmountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self setTableViewRefresh];
}

- (IBAction)withdrawalBtnAction:(UIButton *)sender {
    LCWithdrawalViewController * pushVC = [LCWithdrawalViewController xx_instantiateFromStoryboardNamed:@"Mine"];
    [self.navigationController pushViewController:pushVC animated:YES];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)configUI {
    self.amountLb.text = kFORMAT(@"%@",self.amount.value);
    self.affairsBtn.Space = 7;
    self.affairsBtn.BtnStyle = XWXBtnStyleImageRight;
}

- (IBAction)affairsBtnAction:(XWXImgTitleBtn *)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *all = [UIAlertAction actionWithTitle:@"全部明细" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.affairsBtn setTitle:action.title forState:UIControlStateNormal];
        self.affairs = 0;
        self.page = 1;
        [self getAmountListData];
    }];
    [alert addAction:all];
    
    UIAlertAction *taskList = [UIAlertAction actionWithTitle:@"任务明细" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.affairsBtn setTitle:action.title forState:UIControlStateNormal];
        self.affairs = 1;
        self.page = 1;
        [self getAmountListData];
    }];
    [alert addAction:taskList];
    
    UIAlertAction *withdrawal = [UIAlertAction actionWithTitle:@"提现明细" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.affairsBtn setTitle:action.title forState:UIControlStateNormal];
        self.affairs = 2;
        self.page = 1;
        [self getAmountListData];
    }];
    [alert addAction:withdrawal];
    
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)setTableViewRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getAmountListData];
    }];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getAmountListData];
    }];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    
    self.tableView.ly_emptyView = [LCEmptyView emptyViewWithImage:nil titleStr:@"暂无数据～" detailStr:nil];
}

-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)getAmountListData {
    if (self.affairs == 0) {
        [self getAllListData];
    } else if (self.affairs == 1) {
        [self getTaskListData];
    } else {
        [self getTiXianListData];
    }
}

- (void)getAllListData {
    [LCMineService amountAllListWithPage:self.page Block:^(BOOL isSuccess, NSArray<LCAmountListModel *> *list, NSInteger total) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (isSuccess) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

- (void)getTaskListData {
    [LCMineService amountTaskListWithPage:self.page Block:^(BOOL isSuccess, NSArray<LCAmountListModel *> *list, NSInteger total) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (isSuccess) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

- (void)getTiXianListData {
    [LCMineService amountTixianListWithPage:self.page Block:^(BOOL isSuccess, NSArray<LCAmountListModel *> *list, NSInteger total) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (isSuccess) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 76;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCAmountListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCAmountListTableViewCell"];
    LCAmountListModel * amount = self.dataArr[indexPath.row];
    if (self.affairs == 2 && [amount.affairs integerValue] == 100) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"LCAmountListTableViewCellDrawal"];
    }
    
    if (self.dataArr.count <= 1) {
        [cell.bgView quickSetViewRoundCornWithCorneradius:5 andDerection:UIRectCornerAllCorners];
        cell.lineView.hidden = YES;
    } else {
        if (indexPath.row == 0) {
            [cell.bgView quickSetViewRoundCornWithCorneradius:5 andDerection:UIRectCornerTopLeft|UIRectCornerTopRight];
            cell.lineView.hidden = NO;
        } else if (indexPath.row == self.dataArr.count - 1) {
            [cell.bgView quickSetViewRoundCornWithCorneradius:5 andDerection:UIRectCornerBottomLeft|UIRectCornerBottomRight];
            cell.lineView.hidden = YES;
        } else {
            [cell.bgView quickSetViewRoundCornWithCorneradius:0 andDerection:UIRectCornerAllCorners];
            cell.lineView.hidden = NO;
        }
    }
    
    cell.affairs = self.affairs;
    cell.amount = self.dataArr[indexPath.row];
    
    return cell;
}


@end
